package cn.com.dhcc.turbo.web.controller.exch;

import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dhcc.app.core.annotation.SiNoNeedLogin;
import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.NetUtil;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.app.core.util.XstreamUtil;
import cn.com.dhcc.app.pub.core.consts.AppConst.PostheadStatus;
import cn.com.dhcc.app.pub.core.consts.KernelParam;
import cn.com.dhcc.turbo.entity.api.Exch;
import cn.com.dhcc.turbo.entity.api.SendResp;
import cn.com.dhcc.turbo.service.exch.HttpDataExchService;

@Controller
@RequestMapping("/api")
public class DataExchController extends BaseController {
	@SuppressWarnings("deprecation")
	private Logger logger = Log.getDetachLogger(LOGTYPE.TURBO);
	
	@SuppressWarnings("deprecation")
	private Logger accessLogger = Log.getDetachLogger(LOGTYPE.ACCESS);
	
	@Autowired
	HttpDataExchService service;
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/exch",produces={"text/xml;charset=UTF-8"})
	@SiNoNeedLogin
	@ResponseBody
	public String exch(HttpServletRequest request,HttpServletResponse response, String exch){
		String ip=NetUtil.getRemoteIp(request);
		Map<String,Object> map=null;
		try {
			if (KernelParam.CMDATA_MAXSIZE<=request.getContentLength()) {
				SendResp sendResp=new SendResp();
				sendResp.setGroupId(UUIDGenerator.getUUID());
				sendResp.setStatus(PostheadStatus.BAD_REQUEST.value());
				sendResp.setMessage("发送的消息过大，不允许超过"+KernelParam.CMDATA_MAXSIZE+"B");
				try {
					return XstreamUtil.toXml(sendResp);
				} catch (Exception e) {
					logger.error(e);
				}
			}
			
			if (exch==null || "".equals(exch)) {//试着取第一个参数的值作为exch的值
				map=request.getParameterMap();//获取参数信息
			    Set<String> set=map.keySet();
			    for (String key : set) {
					if (map.get(key) instanceof String[]) {
						exch=((String[]) map.get(key))[0];
						break;
					}
				}
			}
			Exch exchObj=XstreamUtil.toObject(Exch.class, exch);
			exchObj.setIp(ip);
			accessLogger.info("[" + exchObj.getIp() + "]请求调用方法：" + exchObj.getActionName() 
				+ ";请求参数：(" + StringEscapeUtils.unescapeHtml4(exch) + ")");
			return service.proccess(exchObj);
		} catch (Exception e) {
			SendResp sendResp=new SendResp();
			sendResp.setGroupId(UUIDGenerator.getUUID());
			sendResp.setStatus(PostheadStatus.BAD_REQUEST.value());
			sendResp.setMessage("参数异常，请检查exch参数信息【"+exch+"】");
			try {
				return XstreamUtil.toXml(sendResp);
			} catch (Exception e1) {
				logger.error(e);
			}
		}
		return null;
	}
	

}
